#pragma once
// MySQL版本
#include <mysql/mysql.h>
#include <iostream>
#include <string>
#include <vector>
#include <cassert>

#include "../comm/log.hpp"
#include "../comm/util.hpp"

namespace ns_model
{
    using namespace std;
    using namespace ns_log;
    using namespace ns_util;

    struct Question
    {
        string number; // 题目编号，唯一的
        string name;   // 标题
        string star;   // 难度：简单、中等、困难
        string desc;   // 题目描述
        string header; // 为用户预设的代码
        string tail;   // 题目的测试用例，和header拼接形成完整代码
        int cpu_limit; // 时间要求(S)
        int mem_limit; // 空间要求(KB)
    };

    const string oj_questions = " oj_questions ";
    const string host = "127.0.0.1";
    const string user = "oj_client";
    const string passwd = "123456";
    const string db = "oj";
    const uint32_t port = 3306;

    class Model
    {
    public:
        Model()
        {
        }
        ~Model() {}

        bool QueryMysql(const string &sql, vector<Question> *out)
        {
            // 创建mysql句柄
            MYSQL *mfp = mysql_init(nullptr);
            // 连接数据库
            if (nullptr == mysql_real_connect(mfp, host.c_str(), user.c_str(), passwd.c_str(), db.c_str(), port, nullptr, 0))
            {
                LOG(FATAL) << "连接数据库失败！" << endl;
                return false;
            }
            LOG(INFO) << "连接数据库成功...执行: " << sql << endl;
            // 设置连接的编码格式
            mysql_set_character_set(mfp, "utf8");

            // 执行sql语句
            if (mysql_query(mfp, sql.c_str()) != 0)
            {
                LOG(WARNING) << "sql语句执行出错：" << sql << endl;
                return false;
            }

            // 获取执行结果，存放在堆上
            MYSQL_RES *res = mysql_store_result(mfp);
            // 分析结果
            int rows = mysql_num_rows(res); // 获取行数

            struct Question q;
            for (int i = 0; i < rows; i++)
            {
                MYSQL_ROW row = mysql_fetch_row(res);
                q.number = row[0];
                q.name = row[1];
                q.star = row[2];
                q.desc = row[3];
                q.header = row[4];
                q.tail = row[5];
                q.cpu_limit = stoi(row[6]);
                q.mem_limit = stoi(row[7]);

                out->push_back(q);
            }

            // 释放结果空间
            mysql_free_result(res);
            // 关闭连接
            mysql_close(mfp);

            return true;
        }

        bool GetAllQuestions(vector<struct Question> *out)
        {
            string sql = "select * from ";
            sql += oj_questions;
            return QueryMysql(sql, out);
        }
        bool GetOneQuestion(const string &number, struct Question *q)
        {
            bool ret = false;
            string sql = "select * from ";
            sql += oj_questions;
            sql += "where number=";
            sql += number;
            vector<Question> result;
            if (QueryMysql(sql, &result))
            {
                if (result.size() == 1)
                {
                    *q = result[0];
                    ret = true;
                }
            }

            return ret;
        }
    };

} // namespace ns_model
